package com.cpi.cornpp.purchaseorder.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.form.PurchaseOrderVersionChargeForm;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderCostVersionService;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderCostVersionView;
import com.cs.common.utils.NumberUtil;

public class PurchaseOrderVersionChargeAction extends MappingDispatchAction {
	private static final String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	private static final String NOCOST_FORWARD = "nocost";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String PURCHASE_ORDER_VERSION_ID = "purchaseOrderVersionId";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String COST_ELEMENT_ID = "costElementId";//$NON-NLS-1$
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	private static final String ERROR_NO_COST = "common.nocost.error";//$NON-NLS-1$
	private static final String COST_ELEMENT_LIST = "costElementsList";//$NON-NLS-1$
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		HttpSession httpSession = null;
		String purchaseOrderId = null;
		PurchaseOrderVersionChargeForm purchaseOrderVersionChargeForm = null;
		CostElementServices costElementServices = null;
		List costElementViews = null;
		MessageResources messageResources = null;
		String messageError = null;
		String forward = DEFAULT_FORWARD;
		try{
			costElementServices = new CostElementServices();
			httpSession = request.getSession();
			purchaseOrderId =request.getParameter(PURCHASE_ORDER_VERSION_ID);
			costElementViews = (List)costElementServices.getCostElementList(NumberUtil.parseInt(purchaseOrderId));
			if( costElementViews != null  && costElementViews.size() > 0 ){
				purchaseOrderVersionChargeForm = (PurchaseOrderVersionChargeForm)form;
				if( purchaseOrderVersionChargeForm.getPurchaseOrderChargeVersionView() == null ){
					purchaseOrderVersionChargeForm.setPurchaseOrderChargeVersionView(new PurchaseOrderCostVersionView());
				}
				purchaseOrderVersionChargeForm.getPurchaseOrderChargeVersionView().setPurchaseOrderVersionId(new Integer(NumberUtil.parseInt(purchaseOrderId)));
				httpSession.setAttribute(PURCHASE_ORDER_VERSION_ID, purchaseOrderId);
				request.setAttribute(COST_ELEMENT_LIST, costElementViews);
			}else{
				messageResources = getResources(request);
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), ERROR_NO_COST);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = NOCOST_FORWARD;
			}
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(forward);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		HttpSession httpSession = null;
		PurchaseOrderCostVersionService purchaseOrderCostVersionService = null;
		PurchaseOrderVersionChargeForm purchaseOrderVersionChargeForm = null;
		String messageError = null;
		MessageResources messageResources = null;
		String forward = DEFAULT_FORWARD;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			purchaseOrderCostVersionService = new PurchaseOrderCostVersionService();
			purchaseOrderVersionChargeForm = (PurchaseOrderVersionChargeForm)form;
			purchaseOrderCostVersionService.addPurchaseOrderCostVersion(purchaseOrderVersionChargeForm.getPurchaseOrderChargeVersionView());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (PurchaseProgramException purchaseProgramException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), purchaseProgramException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
				form.reset(mapping, request);
				throw purchaseProgramException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
			}
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		HttpSession httpSession = null;
		String purchaseOrderVersionId = null;
		String costElementId = null;
		PurchaseOrderCostVersionService purchaseOrderCostVersionService = null;
		PurchaseOrderCostVersionView purchaseOrderChargeVersionView =  null;
		String messageError = null;
		MessageResources messageResources = null;
		try{
			messageResources = getResources(request);
			purchaseOrderChargeVersionView = new PurchaseOrderCostVersionView();
			purchaseOrderCostVersionService = new PurchaseOrderCostVersionService();
			httpSession = request.getSession();
			purchaseOrderVersionId = request.getParameter(PURCHASE_ORDER_VERSION_ID);
			costElementId = request.getParameter(COST_ELEMENT_ID);
			purchaseOrderChargeVersionView.setPurchaseOrderVersionId(new Integer(purchaseOrderVersionId));
			purchaseOrderChargeVersionView.setCostElementId(new Integer(costElementId));
			purchaseOrderCostVersionService.deletePurchaseOrderCostVersion(purchaseOrderChargeVersionView);
			httpSession.setAttribute(PURCHASE_ORDER_VERSION_ID, purchaseOrderVersionId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (PurchaseProgramException purchaseProgramException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
}